1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10 using System.Globalization;
11
12 namespace SoftQuanLyNhaHang.Views
13 {
14 public partial class uctCongNo : UserControl
15 {
16 public uctCongNo()
17 {
18 InitializeComponent();
19 }
20
21 //khởi tạo gọi trong menuscript
22 public static uctCongNo uctCN = new uctCongNo();
23
24 int _IdCongNo = 0;
25 string _TieuDe = string.Empty;
26 string _HinhThuc = "Thu";
27 string _LoaiThuChi= string.Empty;
28 DateTime _NgayTao = DateTime.Now;
29 string _TrangThai = "SuDung";
30 double _SoTien = 0;
31 float _Thue = 0;
32 string _DienGiai = string.Empty;
33 string _LyDo = string.Empty;
34 string _NhanVien= string.Empty;
35 string _DuocPheDuyet = string.Empty;
36 string _GhiChu = string.Empty;
37 int _NhapHangId = 0;
38
39 int _pageSize = 30;
40 int _pageIndex = 1;
41 int _tongSoBanGhi = 0;
42 int _stt = 0;
43
44 string format_BatDau = "dd/MM/yyyy";
45 DateTime dateTime_BatDau;
46 DateTime ngayBatD = DateTime.Now;
47 string format_KetThuc = "dd/MM/yyyy";
48 DateTime dateTime_KetThuc;
49 DateTime denNgay = DateTime.Now;
50 string hthuc = "";
51 string tie_de = string.Empty;
52
53 /*
54 private void HienThi_Thue_ComboBox()
55 {
56 Dictionary<string, string> comboSource = new Dictionary<string, string>();
57
58 DataTable dtThue = new DataTable();
59 dtThue = Controllers.ThueCtrl.FillDataSetThue_GetShowDropdow().Tables[0];
60
61 if (dtThue.Rows.Count > 0)
62 {
63 foreach (DataRow item in dtThue.Rows)
64 {
65 comboSource.Add(item["Thue"].ToString(), item["Thue"].ToString());
66 }
67
68 }
69 cmbThue.DataSource = new BindingSource(comboSource, null);
70 cmbThue.DisplayMember = "Value";
71 cmbThue.ValueMember = "Key";
72 }
73 */
74 /*
75 private void HienThi_LoaiThuChi_ComboBox()
76 {
77 Dictionary<string, string> comboSource = new Dictionary<string, string>();
78
79 comboSource.Add("NhapHang","Nhập hàng");
80 comboSource.Add("BanHang", "Bán hàng");
81 comboSource.Add("Khac", "Khác");
82
83 cmbLoaiThuChi.DataSource = new BindingSource(comboSource, null);
84 cmbLoaiThuChi.DisplayMember = "Value";
85 cmbLoaiThuChi.ValueMember = "Key";
86 }
87 */
88 private void btnThucHien_Click(object sender, EventArgs e)
89 {
90 _stt = _pageIndex * _pageSize - _pageSize;
91
92 if (string.IsNullOrEmpty(txtSoTien.Text))
93 {
94 MessageBox.Show("Số tiền không được để trống");
95 txtSoTien.Focus();
96 return;
97 }
98 try
99 {
100 _TieuDe = txtTieuDe.Text.Trim();
101
102 }
103 catch
104 {
105
106 }
107
108 try
109 {
110 if(radioButtonThu.Checked == true)
111 {
112 _HinhThuc = "Thu";
113 }
114 }
115 catch
116 {
117
118 }
119 if (radioButtonChi.Checked == true)
120 {
121 _HinhThuc = "Chi";
122 }
123
124 try
125 {
126 _LoaiThuChi = "";// cmbLoaiThuChi.SelectedValue.ToString();
127
128 //if (_LoaiThuChi == "Khac")
129 //{
130 //
131 // _LoaiThuChi = txtLoaiThuChi.Text.Trim();
132 //}
133 }
134 catch { }
135
136 try
137 {
138 _NgayTao = DateTime.ParseExact(dateTimePickerNgay.Text, "dd/MM/yyyy", null);
139
140 }
141 catch { }
142
143 try
144 {
145 _SoTien = CauHinhHeThong.Format_SoDeTinhToan(txtSoTien.Text.Trim(),true);
146 }
147 catch { }
148
149 try
150 {
151 _Thue = 0;// float.Parse(cmbThue.SelectedValue.ToString());
152 }
153 catch { }
154
155 try
156 {
157 _DienGiai = txtDienGiai.Text.Trim();
158 }
159 catch { }
160
161
162 // try
163 // {
164 // _GhiChu = txtGhiChu.Text.Trim();
165 //}
166 // catch { }
167 int i = 0;
168 if (_IdCongNo == 0)
169 {
170 i = Controllers.CongNoCtrl.InsertCongNo(_IdCongNo, _TieuDe, _LoaiThuChi, _NgayTao, _TrangThai, _SoTien, _Thue, _DienGiai, _LyDo, _NhanVien, _DuocPheDuyet, _GhiChu, _HinhThuc, _NhapHangId);
171
172 }
173 else
174 {
175
176 Controllers.CongNoCtrl.UpdateCongNo(_IdCongNo, _TieuDe, _LoaiThuChi, _NgayTao, _TrangThai, _SoTien, _Thue, _DienGiai, _LyDo, _NhanVien, _DuocPheDuyet, _GhiChu, _HinhThuc, _NhapHangId);
177
178
179 }
180
181
182 this.HienThiDsCongNo();
183
184 tabControl1.SelectedTab = tabPage1;
185
186 }
187
188 private void txtSoTien_TextChanged(object sender, EventArgs e)
189 {
190 try
191 {
192 string value = txtSoTien.Text.Replace(CauHinhHeThong.PHAN_CACH_HANG_NGHIN, "");
193 ulong ul = 0;
194
195 if (ulong.TryParse(value, out ul))
196 {
197 txtSoTien.TextChanged -= txtSoTien_TextChanged;
198
199 if (txtSoTien.Text.StartsWith("0"))
200 {
201 txtSoTien.Text = ul.ToString();
202 }
203 else
204 {
205 txtSoTien.Text = CauHinhHeThong.DoFormat_NhapSoTrongText(value);
206 }
207
208 txtSoTien.SelectionStart = txtSoTien.Text.Length;
209 txtSoTien.TextChanged += txtSoTien_TextChanged;
210 }
211 else
212 {
213 if (txtSoTien.Text.StartsWith(CauHinhHeThong.DAU_PHAY))
214 {
215 txtSoTien.TextChanged -= txtSoTien_TextChanged;
216 txtSoTien.Text = "0" + txtSoTien.Text;
217 txtSoTien.SelectionStart = txtSoTien.Text.Length;
218 txtSoTien.TextChanged += txtSoTien_TextChanged;
219 }
220 }
221
222 lblChuTien.Text = CauHinhHeThong.HienThiGiaTien_ThanhChu((decimal)CauHinhHeThong.Format_SoDeTinhToan(txtSoTien.Text.Trim(),true));
223 }
224 catch
225 {
226 }
227
228
229
230 }
231
232 private void txtSoTien_KeyPress(object sender, KeyPressEventArgs e)
233 {
234 CauHinhHeThong.NhapSoTrongTextBox(sender, e);
235 }
236
237 private void uctCongNo_Load(object sender, EventArgs e)
238 {
239 //this.HienThi_LoaiThuChi_ComboBox();
240
241 //this.HienThi_Thue_ComboBox();
242
243
244 this.HienThiDsCongNo();
245
246 maskedTextBoxDenNgay.Text = CauHinhHeThong.Format_HienThiNgayThangNam(DateTime.Now.ToShortDateString(), false);
247 }
248
249 private void HienThiDsCongNo()
250 {
251
252 if (DateTime.TryParseExact(maskedTextBoxTuNgay.Text, format_BatDau, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime_BatDau))
253 {
254 ngayBatD = DateTime.ParseExact(maskedTextBoxTuNgay.Text, "dd/MM/yyyy", null);
255 }
256 else
257 {
258 ngayBatD = DateTime.Parse("2000-01-01");
259 }
260
261
262
263 if (DateTime.TryParseExact(maskedTextBoxDenNgay.Text, format_KetThuc, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime_KetThuc))
264 {
265 denNgay = DateTime.ParseExact(maskedTextBoxDenNgay.Text, "dd/MM/yyyy", null);
266 }
267 else
268 {
269 denNgay = DateTime.Now;
270 }
271
272
273 /*
274 if (radioButtonChi.Checked == true && radioButtonThu.Checked == true)
275 {
276 hthuc = "";
277 }
278 else if (radioButtonChi.Checked == false && radioButtonThu.Checked == false)
279 {
280 hthuc = "";
281 }
282 else if (radioButtonChi.Checked == true && radioButtonThu.Checked == false)
283 {
284 hthuc = "Chi";
285 }
286 else
287 {
288 hthuc = "Thu";
289 }
290 */
291
292 hthuc = "";
293
294 if (chkChi.Checked == true)
295 {
296 hthuc += "Chi";
297 }
298 else
299 {
300 hthuc += "";
301 }
302
303 if (chkThu.Checked == true)
304 {
305 hthuc += "Thu";
306 }
307 else
308 {
309 hthuc += "";
310 }
311
312 tie_de = txtTieuDe_TimKiem.Text.Trim();
313
314 dgvDsCongNo.Rows.Clear();
315
316 //Ẩn cột đầu tiên
317 dgvDsCongNo.RowHeadersVisible = false;
318 //-https://stackoverflow.com/questions/17404969/right-align-a-column-in-datagridview-doesnt-work
319 this.dgvDsCongNo.Columns["STT"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
320 this.dgvDsCongNo.Columns["STT"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
321 //this.dgvDsCongNo.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
322
323 this.dgvDsCongNo.Columns["HinhThuc"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
324 this.dgvDsCongNo.Columns["HinhThuc"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
325
326 this.dgvDsCongNo.Columns["NgayTao"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
327 this.dgvDsCongNo.Columns["NgayTao"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
328
329 this.dgvDsCongNo.Columns["SoTien"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
330 this.dgvDsCongNo.Columns["SoTien"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
331
332 this.dgvDsCongNo.Columns["ThayDoi"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
333 this.dgvDsCongNo.Columns["ThayDoi"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
334
335
336 //select từ bảng kho
337 DataTable dtCn = new DataTable();
338
339 dtCn = Controllers.CongNoCtrl.FillDataSet_CongNo_Search(_IdCongNo, tie_de, _LoaiThuChi, ngayBatD, _TrangThai, _SoTien, _Thue, _DienGiai, _LyDo, _NhanVien, _DuocPheDuyet, _GhiChu, hthuc, _NhapHangId, denNgay, _pageSize, _pageIndex).Tables[0];
340
341
342 if (dtCn.Rows.Count > 0)
343 {
344
345
346 _tongSoBanGhi = Int32.Parse(dtCn.Rows[0]["TongSoBanGhi"].ToString());
347
348 if (chkThu.Checked == true)
349 {
350 txtTongThu.Text = CauHinhHeThong.Format_SoDeHienThi(CauHinhHeThong.Format_SoDeTinhToan(dtCn.Rows[0]["TongTienThu"].ToString(), false), true);
351 }
352 else
353 {
354 txtTongThu.Text = "";
355 }
356
357 if (chkChi.Checked == true)
358 {
359 txtTongChi.Text = CauHinhHeThong.Format_SoDeHienThi(CauHinhHeThong.Format_SoDeTinhToan(dtCn.Rows[0]["TongTienChi"].ToString(), false), true);
360 }
361 else
362 {
363 txtTongChi.Text = "";
364 }
365
366
367
368 double sotien = 0;
369 string loaithuc = "NhapHang";
370
371 foreach (DataRow item in dtCn.Rows)
372 {
373 _stt += 1;
374
375 int n = dgvDsCongNo.Rows.Add();
376
377 dgvDsCongNo.Rows[n].Cells["IdCongNo"].Value = item["IdCongNo"].ToString();
378 dgvDsCongNo.Rows[n].Cells["STT"].Value = _stt.ToString();
379
380 loaithuc = item["LoaiThuChi"].ToString();
381
382 /*
383 if (loaithuc == "NhapHang")
384 {
385 loaithuc = "Nhập hàng";
386 }
387 else if (loaithuc == "BanHang")
388 {
389 loaithuc = "Bán hàng";
390 }else{
391 loaithuc = item["LoaiThuChi"].ToString();
392 }
393 */
394
395 dgvDsCongNo.Rows[n].Cells["LoaiThuChi"].Value = loaithuc;
396
397 dgvDsCongNo.Rows[n].Cells["TieuDe"].Value = item["TieuDe"].ToString();
398 dgvDsCongNo.Rows[n].Cells["NgayTao"].Value = CauHinhHeThong.Format_HienThiNgayThangNam(item["NgayTao"].ToString(), false);
399 dgvDsCongNo.Rows[n].Cells["NgayTao2"].Value = item["NgayTao"].ToString();
400
401
402 sotien = CauHinhHeThong.Format_SoDeTinhToan(item["SoTien"].ToString(),false);
403 dgvDsCongNo.Rows[n].Cells["SoTien"].Value = CauHinhHeThong.Format_SoDeHienThi(sotien, true);
404
405 dgvDsCongNo.Rows[n].Cells["Thue"].Value = item["Thue"].ToString();
406
407 dgvDsCongNo.Rows[n].Cells["DienGiai"].Value = item["DienGiai"].ToString();
408 dgvDsCongNo.Rows[n].Cells["HinhThuc"].Value = item["HinhThuc"].ToString();
409 dgvDsCongNo.Rows[n].Cells["GhiChu"].Value = item["GhiChu"].ToString();
410
411 dgvDsCongNo.Rows[n].Cells["ThayDoi"].Value = "Sửa";
412 }
413 }
414 else
415 {
416 _tongSoBanGhi = 0;
417 txtTongChi.Text = "";
418 txtTongThu.Text = "";
419 }
420 lblTongSoBanGhiHienThi.Text = CauHinhHeThong.HienThiSoBanGhiTuA_DenB(_pageSize, _pageIndex, _stt.ToString(), _tongSoBanGhi.ToString());
421
422 }
423
424 private void linkLocNangCao_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
425 {
426 frmCongNo_LocNangCao frmCnLnc = new frmCongNo_LocNangCao();
427 frmCnLnc.ShowDialog();
428 }
429
430 private void btnTimKiem_Click(object sender, EventArgs e)
431 {
432 _pageSize = 30;
433 _pageIndex = 1;
434 _stt = 0;
435
436 this.clearForm();
437
438 this.HienThiDsCongNo();
439 }
440
441 private void linkLabel_Them_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
442 {
443 this.clearForm();
444 tabControl1.SelectedTab = tabPage2;
445 }
446
447 private void linkLabel_DanhSach_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
448 {
449
450 this.clearForm();
451 tabControl1.SelectedTab = tabPage1;
452
453
454 }
455
456 private void btnTiep_Click(object sender, EventArgs e)
457 {
458 if (_tongSoBanGhi > (_pageSize * _pageIndex))
459 {
460 _pageIndex = _pageIndex + 1;
461
462 this.HienThiDsCongNo();
463 }
464 else
465 {
466 return;
467 }
468 }
469
470 private void btnSau_Click(object sender, EventArgs e)
471 {
472 if ((_pageSize * _pageIndex) > _pageSize)
473 {
474 _pageIndex = _pageIndex - 1;
475 _stt = _pageSize * _pageIndex - _pageSize;
476
477 this.HienThiDsCongNo();
478 }
479 else
480 {
481 _pageIndex = 1;
482 }
483 }
484
485 private void btnXoa_Click(object sender, EventArgs e)
486 {
487 DialogResult dr = MessageBox.Show("Bạn có chắc chắn xóa?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
488 if (dr == DialogResult.Yes)
489 {
490 //Xóa thu chi
491 Controllers.CongNoCtrl.DeleteCongNo(_IdCongNo);
492
493 _stt = _pageIndex * _pageSize - _pageSize;
494
495 if (_stt == _pageSize)
496 {
497 _pageIndex = _pageIndex - 1;
498 _stt = _pageSize - _stt;
499 }
500
501 this.HienThiDsCongNo();
502
503 tabControl1.SelectedTab = tabPage1;
504 }
505 else
506 {
507 txtTieuDe.Focus();
508 return;
509 }
510
511 }
512
513 private void dgvDsCongNo_CellClick(object sender, DataGridViewCellEventArgs e)
514 {
515 if (dgvDsCongNo.Columns[e.ColumnIndex].Name == "ThayDoi" && e.RowIndex != -1)
516 {
517 //-https://stackoverflow.com/questions/12762036/datagridview-cell-click-event
518 if (dgvDsCongNo.CurrentCell != null && dgvDsCongNo.CurrentCell.Value != null)
519 {
520 _IdCongNo = Int32.Parse(dgvDsCongNo.Rows[e.RowIndex].Cells["IdCongNo"].Value.ToString());
521
522 txtTieuDe.Text = dgvDsCongNo.Rows[e.RowIndex].Cells["TieuDe"].Value.ToString();
523 string hinhThuc = dgvDsCongNo.Rows[e.RowIndex].Cells["HinhThuc"].Value.ToString();
524
525 if (hinhThuc == "Thu")
526 {
527 radioButtonThu.Checked = true;
528 }
529 else
530 {
531 radioButtonChi.Checked = true;
532 }
533
534 txtSoTien.Text = dgvDsCongNo.Rows[e.RowIndex].Cells["SoTien"].Value.ToString();
535
536 dateTimePickerNgay.Text = dgvDsCongNo.Rows[e.RowIndex].Cells["NgayTao2"].Value.ToString();
537
538 txtDienGiai.Text = dgvDsCongNo.Rows[e.RowIndex].Cells["DienGiai"].Value.ToString();
539 tabControl1.SelectedTab = tabPage2;
540
541
542 }
543 }
544 }
545
546
547 private void clearForm()
548 {
549
550 _IdCongNo = 0;
551
552 txtTieuDe.Text = string.Empty;
553
554
555 txtSoTien.Text = string.Empty;
556
557 dateTimePickerNgay.Text =DateTime.Now.ToString(); // CauHinhHeThong.Format_HienThiNgayThangNam(DateTime.Now.ToShortDateString(), false);
558
559 txtDienGiai.Text = string.Empty;
560 }
561
562 private void tabControl1_Selected(object sender, TabControlEventArgs e)
563 {
564 try
565 {
566
567 }
568 catch
569 {
570 }
571 }
572
573 }
574 }